<?php
/*
Primary Author: Derek Shue
Secondary: Sean Margison, Alex Arey
Updated: 12/10/2011
Module Description: The common controller is responsible for shared views
of course, organization, internships, and volunteer request summaries.
*/

class CommonController extends Zend_Controller_Action
{


	/**
	* Initialize: check permissions
	*/
	public function initAction(){
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$userID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		
	}

	/**
	* Display the course with CID corresponding to 'cid' get parameter
	*/
	public function viewcoursedetailsAction(){


		$query = new Application_Model_DbTable_Courses();
		try{
			$row = $query->getCourse('CID = '.$this->getRequest()->getParam('cid'));
			$this->view->course = $row;
		}catch(Exception $e){
	
			$this->view->course = null;
			}
		}
	/**
	* Display the organization with ID corresponding ot 'orgid' get parameter
	*/
	public function vieworganizationdetailsAction(){

		$query = new Application_Model_DbTable_Organizations();
		$notesTable = new Application_Model_DbTable_Notes();
		$noteForm = new Application_Form_NoteForm();
		$auth = Zend_Auth::getInstance();
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($noteForm->isValid($formData)) {
				if($auth->getIdentity()->getUserRank()>=2){//check admin rights
					$notesTable->updatePartnerNote($noteForm->getValue('id'), $noteForm->getValue('note'));
				}
			}
		}
		try{
			$row = $query->getOrganization('ID = '.$this->getRequest()->getParam('orgid'));
			$this->view->org = $row;
			if($auth->getIdentity()->getUserRank()>=2){//admin note
				$note = $notesTable->getPartnerNote($this->getRequest()->getParam('orgid'));
				$this->view->note = $note;
				$noteForm->populate(array('id'=>$this->getRequest()->getParam('orgid'),'note'=>$note));
				$this->view->noteForm = $noteForm;
			}
		}catch(Exception $e){
			$this->view->org = null;
		}
	}
	public function tagsAction(){
		$form = null;
		$this->view->form = $form;
		$tagnames = new Application_Model_DbTable_Tagnames();
		$this->view->currenttags = $tagnames->getAllTagnames();
		
	}
	/**
	* Display the internship with POSID corresponding to 'posid' get parameter.
	*/	
	public function viewinternshipdetailsAction(){


		$query = new Application_Model_DbTable_InternPositions();
		try{
			$row = $query->getInternPosition('POSID = '.$this->getRequest()->getParam('posid'));
			$this->view->internship = $row;
		}catch(Exception $e){
			$this->view->internship = null;
			}
		}
	
	/**
	* Display the volunteer request with VRID corresponding to 'vrid' get parameter.
	*/
	public function viewvrdetailsAction(){
		$query = new Application_Model_DbTable_VolunteerRequests();
		$vrid = $this->getRequest()->getParam('vrid');
		$vq = new Application_Model_VolunteerQuery();
		try{
			$row = $query->getVolunteerRequest('VRID = '.$vrid);
			$this->view->request = $row;
		}catch(Exception $e){
			$this->view->request = null;
		}
		$this->view->partner = $vq->getPartnerNameForVRID($vrid);
	}
}

?>
